package leetcode.code0020;

import java.util.Stack;

public class Solution2 extends Solution0020 {

	@Override
	public boolean isValid(String s) {
		if (s.length() < 2) {
			return false;
		}
		char[] chars = s.toCharArray();
		Stack<Character> stack = new Stack<>();
		for (char element : chars) {
			if (element == '(' || element == '[' || element == '{') {
				stack.add(element);
			} else {
				if (stack.isEmpty())
					return false;
				char c = stack.pop();
				if ((element == ')' && c != '(') || (element == ']' && c != '[') || (element == '}' && c != '{'))
					return false;
			}
		}
		return stack.isEmpty();
	}

	public static void main(String[] args) {
		Solution2 so = new Solution2();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();
		so.debug5();
	}

}
